package com.ruimei.service.impl;

import com.ruimei.entitys.Exam;
import com.ruimei.entitys.ExamSubject;
import com.ruimei.entitys.ScoreExam;
import com.ruimei.entitys.ScoreSubject;
import com.ruimei.mapper.ExamMapper;
import com.ruimei.service.ExamService;
import com.ruimei.util.ResultMap;
import java.math.BigDecimal;
import java.util.*;
import javax.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class ExamServiceImpl
  implements ExamService
{
  @Resource
  ExamMapper examMapper;
  
  @Cacheable(value={"myCache"}, key="'getExamByGradeId'+ #gradeId")
  public Map<String, Object> getExamByGradeId(String gradeId)
  {
    Map<String, Object> map = new HashMap();
    List<Exam> list = this.examMapper.getExamByGradeId(gradeId);
    
    map.put("status", Boolean.valueOf(true));
    map.put("message", "查询成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", list);
    
    return map;
  }
  
  @Cacheable(value={"myCache"}, key="'getScore'+ #examId +#studentName + #classId")
  public Map<String, Object> getScore(String examId, String studentName, String classId)
  {
      if("".equals(studentName)){
          studentName = null;
      }
    Map<String, Object> map = new HashMap();
    List<ScoreExam> list = this.examMapper.getScore(examId, studentName, classId, null);
    
    map.put("status", Boolean.valueOf(true));
    map.put("message", "查询成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", list);
    
    return map;
  }
  
  @Cacheable(value={"myCache"}, key="'getSubjectScore'+ #examId +#studentName+ #classId + #subjectId")
  public Map<String, Object> getSubjectScore(String examId, String studentName, String classId, String subjectId)
  {
      if("".equals(studentName)){
          studentName = null;
      }
    Map<String, Object> map = new HashMap();
    List<ScoreSubject> list = this.examMapper.getSubjectScore(examId, studentName, classId, subjectId);
    
    map.put("status", Boolean.valueOf(true));
    map.put("message", "查询成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", list);
    
    return map;
  }
  
  @Cacheable(value={"myCache"}, key="'getSubjectByExamId'+ #examId")
  public Map<String, Object> getSubjectByExamId(String examId)
  {
    Map<String, Object> map = new HashMap();
    List<ExamSubject> list = this.examMapper.getSubjectByExamId(examId);
    
    map.put("status", Boolean.valueOf(true));
    map.put("message", "查询成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", list);
    
    return map;
  }


  //  年级前50  前100名数分析
  public Map<String,Object> trend(String classId,String gradeId,String judge){
      Map<String,Object> map =new HashMap<String, Object>();
      //通过年级ID获取年级所有考试成绩
      List<ScoreExam> sList = examMapper.getclassScore(classId,gradeId,Integer.parseInt(judge));
      //获取年级所有考试
      List<Exam> eList = examMapper.getExamByGradeId(gradeId);
      //用于存放年级考试前一百名个数
      //System.out.println(sList.size());
      List<ScoreExam> list = new ArrayList<>();
      for(Exam e:eList){
          List<ScoreExam> studentlist = new ArrayList<>();
          for (ScoreExam s:sList){
              if(s.getExamId().equals(e.getExamId())){
                  //用于存放考试排名前一百名学生
                  studentlist.add(s);
              }
          }

          ScoreExam scoreExam = new ScoreExam();
          Long rank =(long) studentlist.size();
          scoreExam.setGradeRank(rank);
          scoreExam.setExamName(e.getExamName());
          scoreExam.setExamId(e.getExamId());
          scoreExam.setGradeName(e.getGradeName());
          if(studentlist.size() > 0){
              scoreExam.setClassName(studentlist.get(0).getClassName());
          }

          list.add(scoreExam);
      }
      map.put("status",true);
      map.put("message","查询成功");
      map.put("count",list.size());
      map.put("data",list);
      return map;

  }

  @Cacheable(value={"myCache"}, key="'analyzeExam'+ #bExamId + #eExamId + #classId + #gradeId + #judge")
  public Map<String,Object> analyzeExam(String bExamId,String eExamId,String classId,String gradeId,String judge) {
        String one = null;
        String two = null;
        if(!bExamId.isEmpty()){
            Date e1 = examMapper.getExamById(bExamId).getStartDate();
            Date e2 = examMapper.getExamById(eExamId).getStartDate();
            one = bExamId;
            two = eExamId;
            if(!e1.before(e2)){
                one = eExamId;
                two = bExamId;
            }
        }
      Map<String,Object> map =new HashMap<String, Object>();
      /**
       * up 上升最大的
       * down 下降最大的
       * trend 趋势
       */

      if("up".equals(judge) || "down".equals(judge)){
          List<ScoreExam> bList = examMapper.getScore(one,null,classId,null);
          List<ScoreExam> eList = examMapper.getScore(two,null,classId,null);
          List<ScoreExam> list = new ArrayList<>();
          for(ScoreExam bs:bList){
              for (ScoreExam es:eList){
                  if(bs.getStudentId().equals(es.getStudentId()) ){
                      BigDecimal number = es.getTotalScore().subtract(bs.getTotalScore());
                      ScoreExam s = new ScoreExam();
                      s.setStudentName(bs.getStudentName());
                      s.setStudentNumber(bs.getStudentNumber());
                      s.setTotalScore(number);
                      list.add(s);
                  }
              }
          }


              //list 排序
              Collections.sort(list, new Comparator<ScoreExam>(){
                  public int compare(ScoreExam o1, ScoreExam o2) {
                      if("up".equals(judge) ) {

                          return o2.getTotalScore().compareTo(o1.getTotalScore());

                      }else{

                          return o1.getTotalScore().compareTo(o2.getTotalScore());
                      }

                  }
              });



          map.put("status",true);
          map.put("message","查询成功");
          map.put("count",list.size());
          map.put("data",list);

      }else if("100".equals(judge)){

          map = trend(classId,gradeId,judge);

      }else if("50".equals(judge)){

          map = trend(classId,gradeId,judge);

      }else{

          map.put("status",false);
          map.put("message","参数有误");

      }

      return map;
  }
  
  @Cacheable(value={"myCache"}, key="'getScoreByStudentId'+ #studentId + #gradeId + #examId")
  public Map<String, Object> getScoreByStudentId(String studentId, String gradeId, String examId)
  {
    Map<String, Object> map = new HashMap();
    List<ScoreExam> list = this.examMapper.getScoreByStudentId(studentId, examId);
    List<ScoreSubject> slist = this.examMapper.getScoreSubjectByStudentId(studentId, examId);
    List<Exam> elist = this.examMapper.getExamByGradeId(gradeId);
    List<Map<String, Object>> lm = new ArrayList();
    List<Object> cont = new ArrayList<>();
    for (Exam e : elist)
    {
      Map<String, Object> examList = new HashMap();
      Map<String, Object> scoreList = new HashMap();
      Map<String, Object> subjectList = new HashMap();
      for (ScoreExam s : list) {
        if (e.getExamName().equals(s.getExamName()))
        {
          scoreList.put("examName", e.getExamName());
          scoreList.put("score", s.getTotalScore());
          scoreList.put("classRank", s.getClassRank());
          scoreList.put("gradeRank", s.getGradeRank());
        }
      }
      for (ScoreSubject s : slist) {
        if (e.getExamName().equals(s.getExamName())) {
          subjectList.put(s.getSubjectName(), s.getScore());
        }
      }
      subjectList.put("examName", e.getExamName());
      examList.put("scoreList", scoreList);
      examList.put("subjectList", subjectList);
      cont.add(examList);
    }
    map.put("status", Boolean.valueOf(true));
    map.put("message", "查询成功");
    map.put("data", cont);
    
    return map;
  }
  
  @Cacheable(value={"myCache"}, key="'getParentScore'+ #examId + #studentName + #classId + #gradeId")
  public Map<String, Object> getParentScore(String examId, String studentName, String classId, String gradeId)
  {
    Map<String, Object> map = new HashMap();
    Map<String, Object> data = new HashMap();
    data.put("subject", this.examMapper.selectSubject(examId, studentName, classId, gradeId));
    data.put("score", this.examMapper.getScore(examId, studentName, classId, gradeId));
    map.put("message", "success");
    map.put("data", data);
    map.put("status", Boolean.valueOf(true));
    return map;
  }
  
  @Cacheable(value={"myCache"}, key="'detailedResult'+ #studentId + #examId")
  public ResultMap detailedResult(String studentId, String examId)
  {
    return new ResultMap(this.examMapper.findStudentScore(studentId, examId));
  }
}


/* Location:              E:\360MoveData\Users\admin\Desktop\teacher_wap-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes\com\ruimei\service\impl\ExamServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       0.7.1
 */